import http from "../../../utils/request";
import myhttp from "../../../utils/request"
// import datetimepicker from 'datetimepicker';
import check from "wechat-validate"
Page({
  behaviors:[check],
  rules:{
    houseName:[
      {required:true,message:"房屋名称不能为空"},
    ],
    repairItemName:[
      {required:true,message:"维修项目不能为空"},
    ],
    mobile:[
      {required:true,message:"手机号码不能为空"},
      {pattern:/^(?:(?:\+|00)86)?1\d{10}$/,message:"手机号码不符合格式"}
    ],
    appointment:[{required:true,message:"预约日期不能为空"}],
    description:[{required:true,message:"问题描述不能为空"}],
  },
  data: {
    // 时间戳
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    // 选中的房屋名称
    houseName:"",
    houseId:"",
    // 选中的维修项目
    repairItemId:"",
    repairItemName:"",
    // 手机号码
    mobile:"",
    // 日期最小时间
    minDate:new Date().getTime(),
    // maxDate:""
    maxDate:"",
    // 格式化日期
    formatter(type, value) {
      // console.log(type);
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    // 选择上门日期
    appointment:"",
    // 问题描述
    description:""
  },
  onLoad(ev){
    console.log(ev);
    if (ev.id) {
      this.getRepicDetail(ev.id)
    }

    // 获取房屋列表信息
    this.houseListInfo()
    // 获取维修列表
    this.replicListInfo()
    // 获取三个月以内的日期
    this.getDatePicker()

  },
  async getRepicDetail(id){
    const {code,data} = await wx.myRequest.get(`/repair/${id}`)
    if(code !== 10000) return wx.utils.toast("获取详情信息失败")
      console.log(data);
    this.setData({
      // 选中的房屋名称
      houseName:data.houseInfo,
      houseId:data.houseId,
      // 选中的维修项目
      repairItemId:data.repairItemId,
      repairItemName:data.repairItemName,
      // 手机号码
      mobile:data.mobile,
      // 选择上门日期
      appointment:data.appointment,
      // 问题描述
      description:data.description,
      attachment:data.attachment
    })

  },
  // 获取房屋列表
  async houseListInfo(){
    const {code,data} = await myhttp.get("/house")
    if (code !== 10000) return wx.utils.toast("获取房屋列表失败！")
    this.setData({
      houseList:data
    })
  },
  // 选择房屋后生效
  selectHousefn(ev){
    console.log(ev);

    this.setData({
      houseName:ev.detail.name,
      houseId:ev.detail.id
    })

  },
  // 获取维修列表
  async replicListInfo(){
    const {code,data} = await myhttp.get("/repairItem")
    if (code !== 10000) return wx.utils.toast("获取房屋列表失败！")
      this.setData({
        repairItem:data
      })
  },
  // 选择维修信息
  selectRepricfn(ev){
    console.log(ev);
    this.setData({
      repairItemId:ev.detail.id,
      repairItemName:ev.detail.name
    })
  },
  // 打开房屋面板
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  // 关闭房屋
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 打开维修项目
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  // 关闭维修项目
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  // 打开日期面板
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  // 关闭日期面板
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 去维修列表页
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  // 设置日期
  getDatePicker(){
    const threeMonthsLater = new Date();
    let years = threeMonthsLater.getFullYear()
    let month = (threeMonthsLater.getMonth()+4)
    if (month > 12) {
      years+=1
      month-=12
    }
    const day = threeMonthsLater.getDate()
    
    // new Date("2024-12-05").getTime(),
    this.setData({
      maxDate:new Date(`${years}-${month}-${day}`).getTime()
    })
  },
  // 关闭时间控件
  pickCancel(){
    this.setData({dateLayerVisible:false})
  },
  // 点击时间控件的完成按钮
  pickconfirm(ev){
    console.log(ev);
    const nowDate = new Date(ev.detail)
    const years = nowDate.getFullYear()
    const month = (nowDate.getMonth()+1)
    const day = nowDate.getDate()
    this.setData({appointment:`${years}-${month}-${day}`,dateLayerVisible:false})
    
  },
  // 上传图片完成后
  afterRead(ev){
    console.log(ev);
    wx.uploadFile({
      url:myhttp.baseURL + "/upload",
      filePath:ev.detail.file.tempFilePath,
      name:"file",
      header:{
        Authorization:`Bearer ${getApp().token}`
      },
      success:(ev)=>{
        console.log(ev);
        // data: "{"code":10000,"message":"操作成功","data"
        // :{"id":"7077242481500160","url":"http://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/livimini/production/20241002/7077242480480256.jpg"}}"

        this.data.attachment.push({url:JSON.parse(ev.data).data.url,isImage:true,deletable:true,id:JSON.parse(ev.data).data.id})

        this.setData({
          attachment:this.data.attachment
        })
      },
      fail:(ev) =>{
        console.log(ev);

      }
    })
  },
  // 删除图片
  deleteImage(ev){
   console.log(ev.detail.index);
   this.data.attachment.splice(ev.detail.index,1)
   this.setData({attachment:this.data.attachment})
  },
  // 提交保修
  async houseSmub(){
    this.validate()

    const {houseId,repairItemId,mobile,appointment,description,attachment} = this.data

    const {code} = await myhttp.post("/repair",{
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment:attachment.map(item => ({id:item.id,url:item.url}))
    })
    if (code !== 10000) return wx.utils.toast("保修请求失败！")
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  }
  
})
